var token;
var u_id;
var num;
var examId;

var numListTxt = "";
var numSize;
var ques_size;


//接收多值的js函数
function manyValues() {
    var url = window.location.search;
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        key = [token, u_id, num, examId];
        var value = new Array(strs.length);
        for (i = 0; i < strs.length; i++) {
            value[i] = unescape(strs[i].split("=")[1]);
            key[i] = value[i];
        }
    }
}

manyValues();
token = key[0];
u_id = key[1];
num = key[2];
examId = key[3];


f();

function f() {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        contentType: 'application/json; charset=utf-8',
        type: "POST",
        url: "/api/examination/listNumByExamId?examId=" + examId,
        async: false,
        success: function (result) {
            if (result.success) {
                numSize = result.data.length;
                for (let i = 0; i < result.data.length; i++) {
                    numListTxt = numListTxt + '<button class="btn-num" value=' + result.data[i] + '>' + result.data[i] + '</button>';
                }
                $("#div-numList").html(numListTxt)
            } else {
                alert(result.message)
            }
        }
    });
}


for (let i = 0; i < numSize; i++) {
    $($(".btn-num")[i]).click(function () {
        $.ajax({
            headers: {
                'Authorization': token,
            },
            contentType: 'application/json; charset=utf-8',
            type: "POST",
            url: "/api/examination/listStudentAnswerByNumAndExamId",
            data: JSON.stringify({
                "examId": examId,
                "num": $($(".btn-num")[i]).val()
            }),
            async: false,
            success: function (result) {
                let txt = "";
                if (result.success) {
                    ques_size = result.data.length;
                    for (let j = 0; j < result.data.length; j++) {
                        txt = txt + '<p>题目：' + result.data[j].question + '</p>\n' +
                            '    <p>考生答案：' + result.data[j].answer + '</p>\n' +
                            '    <p>评分：\n' +
                            '        <label>\n' +
                            '            <input class="theMark" type="text">\n' +
                            '        </label>\n' +
                            '    </p>' +
                            '    <p><button class="btn-toMark" value=' + result.data[i].id + '>确定</button></p>\n'
                    }
                    $("#div-stuAnswer").html(txt)

                    for (let i = 0; i < ques_size; i++) {
                        $($(".btn-toMark")[i]).click(function () {
                            $.ajax({
                                headers: {
                                    'Authorization': token,
                                },
                                contentType: 'application/json; charset=utf-8',
                                type: "POST",
                                url: "/api/examination/mark",
                                data: JSON.stringify({
                                    "studentQuestionId": $($(".btn-toMark")[i]).val(),
                                    "mark": $($(".theMark")[i]).val()
                                }),
                                async: false,
                                success: function (result) {
                                    alert(result.message)
                                }
                            });
                        });
                    }
                } else {
                    alert(result.message)
                }
            }
        });
    });
}

